<template>
	<mescroll-uni :height="height + 'px'" ref="mescrollRef" @init="mescrollInit" @down="downCallback" @up="upCallback" :up="upOption" :down="downOption">
		<block v-for="(item, index) in singleSessionList" v-if="!item.is_delete" :key="item.notice_id">
			<u-swipe-action @click="clickOption" :show="item.show" :index="index" :options="item.options" @open="openAction" btn-width="160">
				<!--  -->
				<chat-session-item ref="chatSessionItem" @handle="toChatPage" :item="item" :index="index"></chat-session-item>
			</u-swipe-action>
		</block>
	</mescroll-uni>
</template>

<script>
import MescrollMixin from '@/components/mescroll-uni/mescroll-mixins.js';
import chatSessionItem from '@/components/chat/chat-session-item.vue';
import { mapState } from 'vuex';
import { getSingleSessionList, setSingleSessionRead, deleteSingleSessionItem, showSingleSessionListSwipeAction } from '@/store/dispatches.js';
export default {
	mixins: [MescrollMixin], // 使用mixin
	components: { chatSessionItem },
	props: {
		height: {
			default: null,
			type: [String, Number]
		}
	},
	data() {
		return {
			upOption: {
				auto: false
			},
			downOption: {
				auto: false
			}
		};
	},
	computed: {
		...mapState({
			singleSessionList: state => state.iMessage.singleSessionList
		})
	},
	methods: {
		refreshData() {
			this.mescroll && this.mescroll.resetUpScroll(false);
		},
		mescrollInit(mescroll) {
			this.mescroll = mescroll;
			this.$nextTick(() => {
				this.mescroll.resetUpScroll(false);
			});
		},
		upCallback(mescroll) {
			getSingleSessionList(() => {
				this.mescroll.endSuccess(this.singleSessionList.length, false);
			});
		},
		toChatPage(data) {
			showSingleSessionListSwipeAction();
			this.$emit('handle', data);
		},
		deleteSession(index) {
			const notice_id = this.singleSessionList[index].notice_id;
			deleteSingleSessionItem(notice_id);
			this.$nextTick(() => {
				this.$u.api.deleteChatSessionItem(notice_id).then(res => {
					this.mescroll.resetUpScroll(false);
				});
			});
		},
		readSession(index) {
			const chat_user = this.singleSessionList[index].chat_user;
			setSingleSessionRead(chat_user);
			setTimeout(() => {
				this.$u.api.setChatSessionRead(chat_user).then(res => {
					this.mescroll.resetUpScroll(false);
				});
			}, 350);
		},

		clickOption(index, idx) {
			showSingleSessionListSwipeAction();
			this[this.singleSessionList[index].options[idx].mapping](index);
		},
		openAction(index) {
			showSingleSessionListSwipeAction(index);
		}
	}
};
</script>

<style></style>
